cmake_minimum_required(VERSION 3.31)

# 强制设置为32位架构
set(CMAKE_SIZEOF_VOID_P 4)
set(CMAKE_SYSTEM_PROCESSOR "i386")

# 设置32位MinGW编译器路径
set(CMAKE_C_COMPILER "E:/soft/JetBrains/CLion 2025.1.4/bin/mingw/mingw32/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "E:/soft/JetBrains/CLion 2025.1.4/bin/mingw/mingw32/bin/g++.exe")

project(c2eLib C)

set(CMAKE_C_STANDARD 99)

# 强制32位编译选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -march=i386")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -march=i386")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")

# 设置输出目录到output文件夹
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/output)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/output)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/output)

# 设置包含目录
include_directories(include)

# 设置Windows DLL导出
if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)  # 关闭自动导出，使用.def文件
endif()

# 收集所有源文件
file(GLOB_RECURSE CORE_SOURCES "src/core/*.c")
file(GLOB_RECURSE DATATYPE_SOURCES "src/datatype/*.c")

# 编译DLL，使用模块定义文件
add_library(c2eLib SHARED 
    ${CORE_SOURCES}
    ${DATATYPE_SOURCES}
    c2eLib.def
)

# 设置DLL输出名称（去掉lib前缀）
set_target_properties(c2eLib PROPERTIES 
    OUTPUT_NAME "c2eLib"
    PREFIX ""
    LINK_FLAGS "-Wl,--enable-stdcall-fixup"
)

# 编译测试程序
add_executable(test_program src/main.c)
target_link_libraries(test_program c2eLib)

# 编译性能测试程序
add_executable(performance_test src/performance_test.c)
target_link_libraries(performance_test c2eLib)
